package org.example.controller;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.R;
import org.example.pojo.Dept;
import org.example.service.IDeptService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author TonySong
 * @date 2024/4/25 0025
 * @time 15:26
 */
@RestController
@RequestMapping("/dept")
@Slf4j
public class DeptController {

    @Value("${server.port}")
    private Integer port;

    @Resource
    private IDeptService deptService;

    @GetMapping("/list")
    public R findAll(){
        log.info("dept-service8000: "+port);
       return R.success(this.deptService.list());
    }
    @PostMapping("/")
    public R save(@RequestBody  Dept dept){
         if (this.deptService.hasName(dept.getDname())){
             return R.err("对不起，部门名称不能重复！");
        }
        try {
            this.deptService.save(dept);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
        return R.ok();

    }

    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable("id") Long abc){
        try {
            this.deptService.deleteById(abc);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return R.ok().getFlag();

    }

    @GetMapping("/{id}")
    public  R findById(@PathVariable("id") Long id){
        return R.success(this.deptService.get(id));
    }

}
